package proyecto.testcasetool.presentation.details.scenario;

import org.eclipse.core.databinding.ObservablesManager;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.databinding.edit.EMFEditObservables;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import proyecto.testcasetool.TestcasetoolPackage;
import proyecto.testcasetool.presentation.details.DetailsPage;
import proyecto.testcasetool.presentation.editor.pages.FormLayoutFactory;


public class SingularInvalidValueDetailPage extends DetailsPage  {

	protected Text txtValue;
	
	protected ObservablesManager obsManager;
	protected EMFDataBindingContext bindingContext;
	
	public SingularInvalidValueDetailPage(FormToolkit toolkit, EditingDomain editingDomain,
			AdapterFactory adapterFactory) {
		super(toolkit, editingDomain, adapterFactory);
		
		bindingContext = new EMFDataBindingContext();
		obsManager = new ObservablesManager();
		
	}
	
	
	@Override
	protected void createDetailsSection(Composite parent) {
		GridLayout gridLayout = (GridLayout) parent.getLayout();
		gridLayout.verticalSpacing = 5;

		Section s = toolkit.createSection(parent, Section.TITLE_BAR);
		s.setText("Singular value details");
		s.setLayoutData(new GridData(GridData.FILL_BOTH));
		s.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		
		Composite client = toolkit.createComposite(s,SWT.FILL);
		
		s.setClient(client);
		client.setLayout(new GridLayout(2, false));
		toolkit.paintBordersFor(client);
		
		addValueWidget(client);
	}


	private void addValueWidget(Composite client) {
		Label lblValue = toolkit.createLabel(client, "Value", SWT.NONE);
		GridData gd_lblValue = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblValue.verticalIndent = 2;
		lblValue.setLayoutData(gd_lblValue);
		
		txtValue = toolkit.createText(client, "", SWT.NONE);
	
		GridData gd_txtValue = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_txtValue.verticalIndent = 2;
		gd_txtValue.widthHint = 244;
		txtValue.setLayoutData(gd_txtValue);
	}
	
	
	protected void doBinding() {
		
		//  value
		IObservableValue obsValueFeature = EMFEditObservables
				.observeValue(editingDomain, eObject,
						TestcasetoolPackage.Literals.SINGULAR_INVALID_VALUE__VALUE);

		bindingContext.bindValue(SWTObservables.observeText(
				txtValue, SWT.FocusOut), obsValueFeature);
		
		obsManager.addObservable(obsValueFeature);
	}
	
	
	
	public void selectionChanged(IFormPart part, ISelection selection) {
		// controlar que sea la part que esto esperando!!!!
		if (!(selection instanceof IStructuredSelection)) {
			return;
		}
		EObject newEObject = getEObjectFromSelection(selection);
		if (newEObject != null && newEObject != eObject) {
			eObject = newEObject;
			if (eObject != null) {
				
				doBinding();
				
			}
		}
	}

	
	@Override
	public void dispose() {
		super.dispose();
		obsManager.dispose();
	}
}
